package com.sn.author.utils.export;

import com.sn.author.common.exception.BadRequestException;
import com.sn.author.domain.entity.CollectionData;
import com.sn.author.utils.BeanUtils;
import com.sn.author.utils.Export;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.List;

public class CollectionExport extends Export {
    public CollectionExport(List<String> titles, String fileName) {
        super(titles, fileName);
    }

    /**
     * "序号","昵称","用户ID","抖音ID","头像"
     * @param list
     * @return
     */
    @Override
    public Export writeData(List list) {
        if(list.isEmpty()){
            throw new BadRequestException("无数据可下载");
        }

        List<CollectionData> exportList = BeanUtils.coventObject(list,CollectionData.class);

        Sheet sheet = super.wb.getSheetAt(0);
        int cindex = 1;
        for (CollectionData collection : exportList) {
            Row row = sheet.createRow(cindex);//从第二行开始保存数据
            row.createCell(0).setCellValue(collection.getId());
            row.createCell(1).setCellValue(collection.getNickname());
            row.createCell(2).setCellValue(collection.getUid());
            row.createCell(3).setCellValue(collection.getShortId());
            row.createCell(4).setCellValue(collection.getAvatar());
            cindex ++;
        }
        return this;
    }
}
